import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { SwitchLightStrategyStateType } from './typings'
import {
  getMonitoSwitchLightTaskList,
  getMonitoSwitchLightTaskInfo,
  addMonitoSwitchLightTaskInfo,
  updateMonitoSwitchLightTaskInfo,
  deleteMonitoSwitchLightTaskInfo,
  getWeekSunsierDetails,
  getTaskScheduleList
} from '~/api/monitorManagement/loopMonitorBox/switchLightStrategy'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum } from '~/utils/util'

const performActionSelectList = [
  {
    dictKey: 1,
    dictValue: '开灯'
  },
  {
    dictKey: 0,
    dictValue: '关灯'
  }
]

const switchLightStrategy: Module<SwitchLightStrategyStateType, SwitchLightStrategyStateType> = {
  namespaced: true,
  state: {
    performActionSelectList,
    performActionEnum: filterValueEnum(
      performActionSelectList,
      'dictKey',
      'dictValue'
    ) as SwitchLightStrategyStateType['performActionEnum'],
    taskTypeSelectList: [],
    taskTypeEnum: {},
    taskStateSelectList: [],
    taskStateEnum: {}
  },
  getters: {
    performActionSelectList: (state) => state.performActionSelectList,
    performActionEnum: (state) => state.performActionEnum,
    taskTypeSelectList: (state) => state.taskTypeSelectList,
    taskTypeEnum: (state) => state.taskTypeEnum,
    taskStateSelectList: (state) => state.taskStateSelectList,
    taskStateEnum: (state) => state.taskStateEnum
  },
  mutations: {
    SET_TASK_TYPE_SELECT_LIST(state, payload) {
      state.taskTypeSelectList = payload
      state.taskTypeEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as SwitchLightStrategyStateType['taskTypeEnum']
    },
    SET_TASK_STATE_SELECT_LIST(state, payload) {
      state.taskStateSelectList = payload
      state.taskStateEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as SwitchLightStrategyStateType['taskStateEnum']
    }
  },
  actions: {
    // 获取开关灯任务列表
    async GetMonitoSwitchLightTaskList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getMonitoSwitchLightTaskList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取开关灯任务详情（包含周，全天等）
    async GetMonitoSwitchLightTaskInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await getMonitoSwitchLightTaskInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 新增开关灯任务
    async AddMonitoSwitchLightTaskInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await addMonitoSwitchLightTaskInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 修改开关灯任务
    async UpdateMonitoSwitchLightTaskInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await updateMonitoSwitchLightTaskInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 删除开关灯任务
    async DeleteMonitoSwitchLightTaskInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await deleteMonitoSwitchLightTaskInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 获取周设置的日出日落时间明细
    async GetWeekSunsierDetails<S, T>(_: ActionContext<S, T>, params: T) {
      return await getWeekSunsierDetails<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 查询任务类型数据字典
    async GetTaskTypeSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'device_task_type' }).then(
        (res: AxiosResponse) => {
          commit('SET_TASK_TYPE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 查询生效状态数据字典
    async GetTaskStateSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'device_task_state' }).then(
        (res: AxiosResponse) => {
          commit('SET_TASK_STATE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 查询任务时间表
    async GetTaskScheduleList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getTaskScheduleList<T>(params).then((res: AxiosResponse) => res?.data)
    }
  }
}

export default switchLightStrategy
